package hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Judy
 * @data 2022/3/12 16:14
 */
public class HbaseUtil {
    static final String c = "cf";

    //    static{
//        conf = HBaseConfiguration.create();
//    }
    // 放置初始化方法
//    public void initConfig() {
//        conf = HBaseConfiguration.create();// 配置
//        conf.set("hbase.zookeeper.quorum", "192.168.24.128");// zookeeper地址
//        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
//        conf.setInt("hbase.rpc.timeout", 20000);
//        conf.setInt("hbase.client.operation.timeout", 20000);
//        conf.setInt("hbase.client.scanner.timeout.period", 20000);
//        conf.set("hbase.client.ipc.pool.type", "RoundRobinPool");
//    }

    public  String  insertinfo(String ip, String port, String tableName, List<Student> list) throws Exception{
        Connection con = getConnection(ip,port);
        HBaseAdmin admin = (HBaseAdmin)con.getAdmin();
        Table table = con.getTable(TableName.valueOf(tableName));
        boolean b = admin.tableExists(TableName.valueOf(tableName));
        if(!b){
            createTable(admin,tableName);
        }
        insertList(table,list);
        return "添加成功";
    }
    //创建表的方法
    private  void createTable (Admin admin, String tableName) throws Exception {
        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
        HColumnDescriptor descriptor2 = new HColumnDescriptor("cf");
        descriptor.addFamily(descriptor2);
        admin.createTable(descriptor);
    }
    //插入数据的方法
    private  void insertList (Table table, List<Student> list) throws Exception {
        for (Student student : list) {
            Put put = new Put(student.getStudentId().getBytes());
            put.addColumn(c.getBytes(), "studentId".getBytes(), student.getStudentId().getBytes());
            put.addColumn(c.getBytes(), "name".getBytes(), student.getName().getBytes());
            put.addColumn(c.getBytes(), "date".getBytes(), student.getDate().getBytes());
            put.addColumn(c.getBytes(), "number".getBytes(), student.getNumber().getBytes());
            put.addColumn(c.getBytes(), "semester".getBytes(), student.getSemester().getBytes());
            put.addColumn(c.getBytes(), "studentStatus".getBytes(), student.getStudentStatus().getBytes());
            table.put(put);
        }
    }
    //更新数据
    public  String  updateData(String ip, String port, String tableName, List<Student> list) throws Exception{
        Connection con = getConnection(ip,port);
        HBaseAdmin admin = (HBaseAdmin)con.getAdmin();
        Table table = con.getTable(TableName.valueOf(tableName));
        boolean b = admin.tableExists(TableName.valueOf(tableName));
        if(!b){
            createTable(admin,tableName);
        }
        insertList(table,list);
        return "添加成功";
    }

    //获得与hbase的连接
    private static Connection getConnection(String ip, String port) throws Exception {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.56.69");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;
    }

//    public  void main(String[] args) {
//        ArrayList<Book> bookArrayList = new ArrayList<>();
//        Book book = new Book();
//        book.setBooksId("0001");
//
//        book.setName("omk");
//        bookArrayList.add(book);
//        try {
//            insertinfo("niit01","2181","Book",bookArrayList);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }

}

